﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
namespace NCS_CS
{
    public class Hider : Panel
    {
        // Fields
        private Button _button;
        private bool _isReduced = false;
        private Panel _panel = new Panel();
        private int _width;
        private EventHandler IsReducedChanged = null;

        // Events
        //[Description("Event fired when the value of property IsReduced is changed"), Category("Property Changed")]
        //public event EventHandler IsReducedChanged
        //{
        //    add
        //    {
        //        EventHandler handler2;
        //        EventHandler isReducedChanged = this.IsReducedChanged;
        //        do
        //        {
        //            handler2 = isReducedChanged;
        //            EventHandler handler3 = (EventHandler)Delegate.Combine(handler2, value);
        //            isReducedChanged = Interlocked.CompareExchange<EventHandler>(ref this.IsReducedChanged, handler3, handler2);
        //        }
        //        while (isReducedChanged != handler2);
        //    }
        //    remove
        //    {
        //        EventHandler handler2;
        //        EventHandler isReducedChanged = this.IsReducedChanged;
        //        do
        //        {
        //            handler2 = isReducedChanged;
        //            EventHandler handler3 = (EventHandler)Delegate.Remove(handler2, value);
        //            isReducedChanged = Interlocked.CompareExchange<EventHandler>(ref this.IsReducedChanged, handler3, handler2);
        //        }
        //        while (isReducedChanged != handler2);
        //    }
        //}

        // Methods
        private void _button_Click(object sender, EventArgs e)
        {
            this.ToggleState();
        }

        private void _button_MouseEnter(object sender, EventArgs e)
        {
            this._panel.BorderStyle = BorderStyle.Fixed3D;
        }

        private void _button_MouseLeave(object sender, EventArgs e)
        {
            this._panel.BorderStyle = BorderStyle.None;
        }

        private void ButtonMove()
        {
            if (this._button != null)
            {
                this._button.Top = (base.ClientRectangle.Height / 2) - (this._button.Height / 2);
            }
        }

        public void Init()
        {
            this._panel.Dock = DockStyle.Fill;
            while (base.Controls.Count > 0)
            {
                Control control = base.Controls[0];
                base.Controls.Remove(control);
                this._panel.Controls.Add(control);
            }
            this._button = new Button();
            this._button.Width = 5;
            this._button.Height = 20;
            this._button.Cursor = Cursors.Hand;
            this._button.FlatStyle = FlatStyle.System;
            this._button.Text = string.Empty;
            this._button.Click += new EventHandler(this._button_Click);
            this._button.MouseEnter += new EventHandler(this._button_MouseEnter);
            this._button.MouseLeave += new EventHandler(this._button_MouseLeave);
            base.Controls.Clear();
            base.Controls.Add(this._button);
            base.Controls.Add(this._panel);
            this.ButtonMove();
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (!base.DesignMode)
            {
                this.ButtonMove();
            }
            base.OnSizeChanged(e);
        }

        private void ToggleState()
        {
            this._isReduced = !this._isReduced;
            if (this._isReduced)
            {
                this._width = base.Size.Width;
                base.Width = this._button.Width;
                this._panel.Enabled = false;
            }
            else
            {
                base.Width = this._width;
                this._panel.Enabled = true;
            }
            if (this.IsReducedChanged != null)
            {
                this.IsReducedChanged(this, new EventArgs());
            }
        }

        // Properties
        public Panel GetContainer
        {
            get
            {
                return this._panel;
            }
        }

        public bool IsReduced
        {
            get
            {
                return this._isReduced;
            }
            set
            {
                if (this._isReduced != value)
                {
                    this.ToggleState();
                }
            }
        }
    }
}
